#!/usr/bin/python
# Copyright (C) 2003, Charles Wang.
# Author:  Charles Wang <charles@linux.net.cn>
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

import os
import os.path
import stat
import sys

#kernelver = 'KERNELVER'
pythondir = 'usr/lib/python2.4'
bbver = '1.4.1'

ldconfig_cmdfmt = (
"if grep -qs %(libpath)s %(root)s/etc/ld.so.conf; then" +
"    /bin/true;" +
"else" +
"    echo %(libpath)s >> %(root)s/etc/ld.so.conf;" +
"    /usr/sbin/chroot %(root)s /sbin/ldconfig;" +
"fi")

def system(cmd, omitfail = 0):
    print cmd
    r = os.system(cmd)
    if r != 0:
        print "Return %d!" % r
        if not omitfail:
            sys.exit(1)

def tgzinst(bindir, fromdir, tgzfn):
    abstgzfn = os.path.abspath("%s/%s" % (bindir, tgzfn))
    if tgzfn[-4:] == '.tgz' or tgzfn[-7:] == '.tar.gz':
        system("cd %s; tar xzf %s" % (fromdir, abstgzfn))
    elif tgzfn[-5:] == '.tbz2' or tgzfn[-8:] == '.tar.bz2':
        system("cd %s; tar xjf %s" % (fromdir, abstgzfn))
    elif tgzfn[-4:] == '.zip':
        system("cd %s; unzip %s" % (fromdir, abstgzfn))

def rpminst(bindir, fromdir, rpmfn):
    absrpmfn = os.path.abspath("%s/%s" % (bindir, rpmfn))
    system("cd %s; rpm2cpio %s | cpio -i --make-directories" % (fromdir, absrpmfn))

def select(fromdir, todir, flist, rflist):
    system("mkdir -p %s" % todir)
    for f in flist:
        system("mkdir -p %s/%s" % (todir, os.path.dirname(f)))
        if os.path.isdir('%s/%s' % (fromdir, f)):
            system("mkdir -p %s/%s" % (todir, f))
            st = os.stat('%s/%s' % (fromdir, f))
            os.chmod('%s/%s' % (todir, f), st[stat.ST_MODE])
            os.chown('%s/%s' % (todir, f), st[stat.ST_UID], st[stat.ST_GID])
        else:
            system("cp -dp %s/%s %s/%s" % (fromdir, f, todir, os.path.dirname(f)))
    for f in rflist:
        system("mkdir -p %s/%s" % (todir, os.path.dirname(f)))
        system("cp -a %s/%s %s/%s" % (fromdir, f, todir, os.path.dirname(f)))

def tgzselect(bindir, fromdir, todir, tgzfn, flist, rflist):
    system("rm -rf %s; mkdir -p %s" % (fromdir, fromdir))
    tgzinst(bindir, fromdir, tgzfn)
    select(fromdir, todir, flist, rflist)

def rpmselect(bindir, fromdir, todir, rpmfn, flist, rflist):
    system("rm -rf %s; mkdir -p %s" % (fromdir, fromdir))
    rpminst(bindir, fromdir, rpmfn)
    select(fromdir, todir, flist, rflist)
